<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// 应用公共文件
/**
 * 检测管理员是否登录
 * @return integer 0/管理员ID
 */
function is_admin_login()
{
    $admin = session('admin_auth');
    if (empty($admin)) {
        return 0;
    } else {
        return session('admin_auth');
    }
}
function getIP(){
    if (getenv("HTTP_CLIENT_IP"))
        $ip = getenv("HTTP_CLIENT_IP");
    else if(getenv("HTTP_X_FORWARDED_FOR"))
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    else if(getenv("REMOTE_ADDR"))
        $ip = getenv("REMOTE_ADDR");
    else $ip = "Unknow";
    return $ip;
}
/**
 * 清除系统缓存
 */
function clear_cache($directory = null)
{
    $directory = empty($directory) ? ROOT_PATH . 'runtime/cache/' : $directory;
    if (is_dir($directory) == false) {
        return false;
    }
    $handle = opendir($directory);
    while (($file = readdir($handle)) !== false) {
        if ($file != "." && $file != "..") {
            is_dir($directory . '/' . $file) ? clear_cache($directory . '/' . $file) : unlink($directory . '/' . $file);
        }
    }
    if (readdir($handle) == false) {
        closedir($handle);
        rmdir($directory);
    }
}
/**
 * 检查权限
 * @param name string|array  需要验证的规则列表,支持逗号分隔的权限规则或索引数组
 * @param type   string			'admin_url'总后台验证规则 'shop_url'商户后台验证规则
 * @param uid  int           认证用户的id
 * @param relation string    如果为 'or' 表示满足任一条规则即通过验证;如果为 'and'则表示需满足所有规则才能通过验证
 * @return boolean           通过验证返回true;失败返回false
 */
function sp_auth_check($uid,$type='admin_url',$name=null,$relation='or'){
    if(empty($uid)){
        return false;
    }
    $iauth_obj=new \app\common\lib\iAuth();
    if(empty($name)){
        $name=strtolower(MODULE_NAME."/".CONTROLLER_NAME."/".ACTION_NAME);
    }
    return $iauth_obj->check($uid,$type, $name, $relation);
}
/**
 * 获取当前登录的管事员id
 * @return int
 */
function get_current_admin_id(){
    return session("admin_auth")['admin_id'];
}
/**
 * 获取当前登录前台用户id
 * @return int
 */
function sp_get_current_userid(){
    return get_current_userid();
}

function get_current_userid(){

    if(isset($_SESSION['user'])){
        return $_SESSION['user']['id'];
    }else{
        return 0;
    }
}
/**
 * 获取当前登录的管事员id
 * @return int
 */
function sp_get_current_admin_id(){
    return get_current_admin_id();
}

/**
 * 32位随机字符串
 */
function RandNumber32(){
    return md5(uniqid());
}
/**
 * 管理员登录记录
 * @param $log_url 操作URL
 * @param $log_info 记录信息
 */
function adminLog($log_info){
    $add['log_time'] = time();
    $add['admin_id'] = session('admin_auth')["admin_id"];
    $add['log_info'] = $log_info;
    $add['log_ip'] = getIP();
    $add['log_type'] = 0;
    $add['log_url'] = MODULE_NAME."/".CONTROLLER_NAME."/".ACTION_NAME;
    model('AdminLog')->save($add);
}
/**
 * 管理员操作记录
 * @param $log_url 操作URL
 * @param $log_info 记录信息
 */
function adminLog2($log_info){
    $add['log_time'] = time();
    $add['admin_id'] = session('admin_auth')["admin_id"];
    $add['log_info'] = $log_info;
    $add['log_ip'] = getIP();
    $add['log_type'] = 1;
    $add['log_url'] =MODULE_NAME."/".CONTROLLER_NAME."/".ACTION_NAME;
    model('AdminLog')->save($add);
}
/**
 * 导出excel
 * @param $strTable	表格内容
 * @param $filename 文件名
 */
function downloadExcel($strTable,$filename)
{
    header("Content-type: application/vnd.ms-excel");
    header("Content-Type: application/force-download");
    header("Content-Disposition: attachment; filename=".$filename."_".date('Y年m月d日H:i:s',time()).".xls");
    header('Expires:0');
    header('Pragma:public');
    echo '<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'.$strTable.'</html>';
}